package com.zhiyou100.api.java8_date;

import java.time.*;
import java.time.temporal.ChronoField;

public class Demo1 {

    public static void main(String[] args) {
        testLocalDateTime();
    }

    /**
     * java8 新日期：LocalDate  日期
     */
    public static void testLocalDate(){
        //获取一个当前默认时区的日期
        LocalDate date = LocalDate.now();
        System.out.println("localDate = " + date);
        //根据指定的时区获取当前日期
        LocalDate date1 = LocalDate.now(ZoneId.of("GMT-10"));
        System.out.println("date1 = " + date1);
        //根据指定的年 月  日  获取日期
        LocalDate date2 = LocalDate.of(2022, 1, 15);
        System.out.println("date2 = " + date2);
        //LocalDate的操作
        int dayOfYear = date.getDayOfYear();   //当前日期是这一年第几天
        System.out.println("dayOfYear = " + dayOfYear);
        int dayOfMonth = date.getDayOfMonth();//当前日期是当前月第几天
        System.out.println("dayOfMonth = " + dayOfMonth);
        int monthValue = date.getMonthValue();//获取月份
        System.out.println("monthValue = " + monthValue);
        Month month = date.getMonth();//获取月份
        System.out.println("month = " + month);
        boolean result = date.isBefore(date1);//判断date是否在date1之前
        System.out.println("result = " + result);
        boolean b = date.isLeapYear();  //闰年
        System.out.println("b = " + b);
    }

    /**
     * java8 新时间   LocalTime  时间
     */
    public static void testLocalTime(){
        //获取默认系统时区时间
        LocalTime time1 = LocalTime.now();
        System.out.println("time1 = " + time1);
        //指定时区获取时间
        LocalTime time2 = LocalTime.now(ZoneId.of("GMT-8"));
        System.out.println("time2 = " + time2);
        //手动设置时间
        LocalTime time3 = LocalTime.of(18, 59, 59);
        System.out.println("time3 = " + time3);
        int hour = time1.getHour(); //获取 时   分  秒
        //根据指定的字段获取
        long l = time1.getLong(ChronoField.HOUR_OF_AMPM);
        System.out.println("l = " + l);
        //修改  时
        time1 = time1.withHour(15);
        System.out.println("time1 = " + time1);
    }

    /**
     * java8 日期 + 时间  LocalDateTime  时间
     */
    public static void testLocalDateTime(){
        //获取当前默认时区的日期 + 时间
        LocalDateTime ldt = LocalDateTime.now();
        System.out.println("ldt = " + ldt);
        //指定时区获取
        LocalDateTime ldt2 = LocalDateTime.now(ZoneId.of("GMT+2"));
        System.out.println("ldt2 = " + ldt2);
        //手动设定日期加 时间
        LocalDateTime ldt3 = LocalDateTime.of(2021, 10, 21, 21, 21, 21);
        System.out.println("ldt3 = " + ldt3);
        //localDate转LocalDateTime
        LocalDate date = LocalDate.now();
        LocalDateTime ldf4 = date.atTime(16, 25, 53);
        System.out.println("ldf4 = " + ldf4);
        //localTime转LocalDateTime
        LocalTime time = LocalTime.now();
        LocalDateTime ldt5 = time.atDate(LocalDate.of(2021, 10, 21));
        System.out.println("ldt5 = " + ldt5);
        //localDateTime转LocalDate
        LocalDate ld = ldt5.toLocalDate();
        System.out.println("ld = " + ld);
        //localDateTime转LocalTime
        LocalTime lt = ldt5.toLocalTime();
        System.out.println("lt = " + lt);
        //根据指定的字段获取相应内容
        int i = ldt.get(ChronoField.MINUTE_OF_HOUR);
        System.out.println("i = " + i);

    }

}
